package com.example.fw;


import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;


public class ContactHelper extends WebDriverHelperBase{

	public ContactHelper(ApplicationManager manager) {
		super(manager);
		
	}
	
	private SortedListOf<ContactData> cachedContacts;

	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null){
			rebuildCache();
		}
		return cachedContacts;
	}
	
	private void rebuildCache() {
	cachedContacts =  new SortedListOf<ContactData>();
	manager.navigateTo().mainPage();
	List<WebElement> rows = driver.findElements(By.xpath("//tr[@name='entry']"));
	for (WebElement row : rows) {
		List<WebElement> cells = row.findElements(By.tagName("td"));
		String firstName = cells.get(2).getText();
		String lastName = cells.get(1).getText();
		cachedContacts.add(new ContactData().withFirstName(firstName).withLastName(lastName));
	}	
	}

	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().mainPage();
		openCreateContact();
	    fillContactForm(contact);
	    submitContactCreation();
	    gotoHomePage();
		rebuildCache();
	    return this;
		
	}		
	

	public ContactHelper modifyContact(int index, ContactData contact) {
		manager.navigateTo().mainPage();
		initContactModification(index);
        fillContactForm(contact);
        submitContactModification();
        gotoHomePage();
		rebuildCache();
	    return this;
		
	}

	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
	    initContactModification(index);
	    submitDeleteContact();
	    gotoHomePage();
		rebuildCache();
	    return this;
	}

	//***********************************************************************************
	
	public ContactHelper openCreateContact()  {
		click(By.linkText("add new"));
		return this;
	}
	
	public ContactHelper fillContactForm(ContactData contact){
		type(By.name("firstname"), contact.getFirstName());
		type(By.name("lastname"), contact.getLastName());
	    type(By.name("address"), contact.getAddress());
	    type(By.name("home"), contact.getHomeTel());
	    type(By.name("mobile"), contact.getMobileTel());
	    type(By.name("work"), contact.getWorkTel());
	    type(By.name("email"), contact.getEmail());
	    type(By.name("email2"), contact.getEmail2());
	    selectByText(By.name("bday"), contact.getBirthdayDay());
	    selectByText(By.name("bmonth"), contact.getBirthdayMonth());
	    type(By.name("byear"), contact.getBirthdayYear());
	    selectByText(By.name("new_group"), contact.getGroupSelect());
	    type(By.name("address2"), contact.getAddressSec());
	    type(By.name("phone2"), contact.getHomeSec());
	    return this;
	}



	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
	}
	
	public ContactHelper gotoHomePage() {
	    click(By.linkText("home page"));
	    return this;  
	}

	public ContactHelper submitDeleteContact() {
		click(By.xpath("//input[@value='Delete']"));
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//input[@value='Update']"));
		cachedContacts = null;
		return this;	
	}

	public ContactHelper initContactModification(int index) {
		index = index+1;
		click(By.xpath("(//img[@title='Edit'])[" + index + "]"));
		return this;
	
	}

}


